<template>
  <el-dialog
    title="新增"
    :visible.sync="addDialogData.visible"
    :close-on-click-modal="false"
    width="80%"
    top="8vh"
  >
    <el-row>
      <el-form
        :model="dataForm"
        :rules="rules"
        ref="ruleForm"
        label-width="130px"
      >
        <div class="cjrwBg">
          <el-row>
            <el-col :span="24">
              <el-col :span="8">
                <el-form-item label="发起名称" prop="faqiName">
                  <el-input
                    disabled
                    v-model="dataForm.faqiName"
                    placeholder="发起名称"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="16">
                <el-form-item label="任务有效期" required>
                  <el-col :span="11">
                    <el-date-picker
                      v-model="dataForm.rwYxqq"
                      type="date"
                      disabled
                      value-format="yyyy-MM-dd"
                      prop="rwYxqq"
                      placeholder="开始日期"
                      style="width: 100%"
                    ></el-date-picker>
                  </el-col>
                  <el-col :span="2">
                    <div class="text-center">至</div>
                  </el-col>
                  <el-col :span="11">
                    <el-date-picker
                      disabled
                      v-model="dataForm.rwYxqz"
                      type="date"
                      value-format="yyyy-MM-dd"
                      prop="rwYxqz"
                      placeholder="结束日期"
                      style="width: 100%"
                    ></el-date-picker>
                  </el-col>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col :span="24">
              <el-col :span="8">
                <el-form-item label="填报结束日期" prop="signDate">
                  <el-date-picker
                    v-model="dataForm.signDate"
                    :disabled="dataForm.state != 0"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="填报结束日期"
                    style="width: 100%"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
<!--              <el-col :span="8">-->
<!--                <el-form-item label="模板名称" prop="templateId">-->
<!--                  <el-select-->
<!--                    v-model="dataForm.templateId"-->
<!--                    :disabled="dataForm.state != 0"-->
<!--                    placeholder="模板名称"-->
<!--                    class="xiaLa"-->
<!--                    style="width: 107%"-->
<!--                    :clearable="true"-->
<!--                  >-->
<!--                    <el-option-->
<!--                      v-for="(item, index) in categoryList"-->
<!--                      :key="index"-->
<!--                      :label="item.templateName"-->
<!--                      :value="item.id"-->
<!--                    ></el-option>-->
<!--                  </el-select>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
            </el-col>
            <el-col>
              <el-col :span="24" v-if="dataForm.islly == '是'">
                <el-button
                  type="text"
                  style="margin-left: 30px"
                  icon="el-icon-user-solid"
                  @click="addUser('bkhr')"
                ></el-button>
                被考核人：
                <span v-for="(item, index) in shouyuerendataList" :key="index"
                  >{{ item.userName }}、</span
                >
              </el-col>
              <el-col :span="24" v-if="dataForm.islly == '否'">
                <el-button
                  type="text"
                  style="margin-left: 30px"
                  icon="el-icon-user-solid"
                  @click="warBtn()"
                ></el-button>
                被考核人：
                <span
                  v-for="(item, index) in dataForm.zdrwBkhUsers"
                  :key="index"
                  >{{ item.userName }}、</span
                >
              </el-col>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </el-row>
    <el-row>
      <el-col :span="24">
        <!-- <div style="text-align: right; margin-top: 20px">
          <el-button type="primary" plain @click="reuseBtn">复用</el-button>
        </div> -->
        <el-table
          style="width: 100%; margin-top: 20px"
          :data="dataList"
          border
          v-if="showBlock"
        >
          <el-table-column
            type="index"
            width="80"
            header-align="center"
            align="center"
            label="序号"
          ></el-table-column>
          <el-table-column
            prop="renwuName"
            header-align="center"
            align="center"
            label="任务名称"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.renwuName" placeholder="任务名称" />
            </template>
          </el-table-column>
          <el-table-column
            label="难度系数"
            header-align="center"
            align="center"
          >
            <el-table-column
              prop="zycd"
              header-align="center"
              align="center"
              label="重要程度"
            >
              <template slot="header" slot-scope="scope">
                <span style="margin-right: 15px">重要程度</span>
                <el-tooltip class="item" effect="dark" placement="top-start">
                  <div slot="content">
                    说明：<br />
                    1. 重要程度高，是指重点任务对集团层面有重要影响；<br />
                    2. 重要程度较高，是指重点任务对公司层面有重要影响；<br />
                    3. 重要程度一般，是指重点任务对部门层面有重要影响；<br />
                  </div>
                  <i class="el-icon-info" style="color: #3e51a3"></i>
                </el-tooltip>
              </template>
              <template slot-scope="scope">
                <el-select
                  v-model="scope.row.zycd"
                  placeholder="请选择重要程度"
                >
                  <el-option
                    v-for="(item, index) in chengduList"
                    :label="item.type"
                    :value="item.id"
                    :key="index"
                  ></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column
              prop="wcnd"
              header-align="center"
              align="center"
              label="完成难度"
            >
              <template slot="header" slot-scope="scope">
                <span style="margin-right: 15px">完成难度</span>
                <el-tooltip class="item" effect="dark" placement="top-start">
                  <div slot="content">
                    说明：<br />
                    1. 完成难度高，是指重点任务创新性强、协调难度大；<br />
                    2.完成难度较高，是指重点任务有一定创新性、需要争取政策支持；<br />
                    3.完成难度一般，是指重点任务有一定难度，但有专业团队支持，本部门能够独立承担工作；<br />
                  </div>
                  <i class="el-icon-info" style="color: #3e51a3"></i>
                </el-tooltip>
              </template>
              <template slot-scope="scope">
                <el-select
                  v-model="scope.row.wcnd"
                  placeholder="请选择完成难度"
                >
                  <el-option
                    v-for="(item, index) in chengduList"
                    :label="item.type"
                    :value="item.id"
                    :key="index"
                  ></el-option>
                </el-select>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column
            header-align="center"
            align="center"
            label="任务有效期"
            min-width="400"
          >
            <el-table-column
              prop="rwYxqq"
              align="center"
              label="开始时间"
              width="200"
            >
              <template slot-scope="scope">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="scope.row.rwYxqq"
                  style="width: 100%"
                  :pickerOptions="startTimPickerOptions"
                  @focus="handleFocus(scope.row.rwYxqz)"
                ></el-date-picker>
              </template>
            </el-table-column>
            <el-table-column
              prop="rwYxqz"
              align="center"
              label="结束时间"
              width="200"
            >
              <template slot-scope="scope">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="scope.row.rwYxqz"
                  style="width: 100%"
                  @focus="handleFocus(scope.row.rwYxqq)"
                  :pickerOptions="endTimPickerOptions"
                ></el-date-picker>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column
            prop="khrName"
            header-align="center"
            align="center"
            label="考核人"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.khrName">{{scope.row.khrName}}</span>
              <i class="el-icon-edit-outline" style="margin-left: 10px;" @click="chooseBtn(scope.row)"></i>
            </template>
          </el-table-column>
          <el-table-column
            prop="weightset"
            header-align="center"
            align="center"
            label="权重"
          >
            <template slot-scope="scope">
              <el-input
                type="number"
                min="0"
                max="100"
                oninput="value=value.replace(/[^\d]/g,0);if(isNaN(value))value=0;if(value>100)value=100;
                                      if(value<0)value=0;if(value.charAt(0)==0&&value.charAt(1)!='')value=0"
                v-model="scope.row.weightset"
                placeholder="请输入内容"
              />
            </template>
          </el-table-column>
          <el-table-column
            prop="workTarget"
            header-align="center"
            align="center"
            label="工作目标/成效"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.workTarget"
                placeholder="工作目标/成效"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="60"
            header-align="center"
            align="center"
          >
            <template slot="header">
              <el-button
                type="primary"
                icon="el-icon-plus"
                circle
                @click="addBtn()"
                :disabled="dataForm.signDate == null"
              ></el-button>
            </template>
            <template slot-scope="scope">
              <el-button
                type="danger"
                icon="el-icon-minus"
                circle
                :disabled="dataForm.signDate == null"
                @click="delBtn(scope.row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!--  选择人员信息-->
    <UserCommonSelect ref="usercomment" @submitfy="submitfy"></UserCommonSelect>
    <span slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        plain
        @click="submitForm('ruleForm')"
        v-loading.fullscreen.lock="fullscreenLoading"
        >保存</el-button
      >
      <el-button type="primary" plain @click="backBtn">关闭</el-button>
    </span>
    <sortingPerChooseDialog
      :sortingPerChooseDialogData="sortingPerChooseDialogData"
      @sortingPerChooseDialogChild="sortingPerChooseDialogChild"
    ></sortingPerChooseDialog>
    <reuseDialog
      :reuseDialogData="reuseDialogData"
      @reuseDialogChildren="reuseDialogChildren"
    ></reuseDialog>
  </el-dialog>
</template>
<script>
var clickStartTime = "";
import {isExistData} from "@/api/hetongrenwu/zhongdian/renwuchuangjian";
import { getRwfqInfo, updateForCj } from "@/api/hetongrenwu/zhongdian/rwcj";
import UserCommonSelect from "@/views/hetongrenwu/components/usercomment-select";
import { effective } from "@/api/hetongrenwu/hetongyeji/hetongnd";
import reuseDialog from "../cjrw/reuseDialog.vue";
import {getList} from '@/api/hetongrenwu/zhongdian/renyuanfenzu'
import sortingPerChooseDialog from "@/views/hetongrenwu/components/sortingPerChooseDialog.vue";

export default {
  components: {
    UserCommonSelect,
    reuseDialog,
    sortingPerChooseDialog,
  },
  props: {
    addDialogData: {},
  },
  data() {
    return {
      startTimPickerOptions:{
        disabledDate(time, a, b) {
          var startDate = new Date(clickStartTime);
          return time.getTime() > startDate.getTime();
        },
      },
      endTimPickerOptions: {
        disabledDate(time, a, b) {
          var startDate = new Date(clickStartTime);
          return time.getTime() < startDate.getTime();
        },
      },
      shouyuerendataList: [],
      dataList: [],
      dataForm: {
        signDate: null,
      },
      categoryList: [],
      jsonData: "",
      index: 1,
      id: "",
      zdrwBkhUsers: [],
      reuseDialogData: {},
      rules: {
        signDate: [
          {
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        templateId: [
          {
            required: true,
            message: "请选择模板",
            trigger: "change",
          },
        ],
      },
      sortingPerChooseDialogData:{},
      chengduList: [],
      edit: Number,
      fullscreenLoading: false,
      showBlock: true,
    };
  },
  watch: {
    addDialogData: {
      handler(newVal, oldVal) {
        if (newVal.visible) {
          this.fngetRwfqInfo();
          this.getxList();
          this.dataForm.state = 0;
        }
      },
      immediate: true,
      deep: true,
    },
    "dataForm.signDate": {
      handler(newVal, oldVal) {
        if (newVal) {
          this.showBlock = false;
          this.$nextTick((res) => {
            this.showBlock = true;
          });
        }
      },
      immediate: true,
      deep: true,
    },
  },
  created() {
    this.fneffective();
  },
  methods: {
    getxList(){
      getList().then(res=>{
        this.chengduList = res.data.data;
      })
    },
    fngetRwfqInfo() {
      getRwfqInfo().then((res) => {
        this.dataForm = res.data.data;
        this.dataForm.state = 0;
      });
    },
    warBtn() {
      this.$message.warning("您不是绩效考核联络员");
    },
    // 新增保存
    submitForm(formName) {
      if (this.dataForm.islly == "是") {
        this.dataForm.zdrwBkhUsers = this.zdrwBkhUsers;
      }
      var data = this.dataList;
      for(var i=0;i<data.length;i++){
        if(data[i].khrId == ''){
          this.$message.warning('第'+[i+1]+'行请选择考核人')
          return
        }
      }
      this.dataForm.zdrwRenwufaqiDetails = this.dataList;
      if (this.dataForm.zdrwBkhUsers.length == 0) {
        this.$message.warning("请选择被考核人");
      } else {
         var ids = ""
        this.dataForm.zdrwBkhUsers.forEach(ele=>{
          ids = ids + ele.userId + ","
        })
        var params = {
          userIds:ids,
          userLx:3,
          faqiName:this.dataForm.faqiName
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            isExistData(params).then(res=>{
              if(res.data.data.data == ''){
                this.fullscreenLoading = true;
                updateForCj(this.dataForm).then((res) => {
                  this.$message.success("新增成功");
                  this.addDialogData.visible = false;
                  this.$emit("addDialogDataChildren", "返回");
                  this.fullscreenLoading = false;
                  this.backBtn();
                  this.dataForm = {};
                  this.zdrwBkhUsers = [];
                });
              }else{
                this.$message.warning(res.data.data.data+'重复')
              }
            })
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      }
    },
    backBtn() {
      this.dataList = [];
      this.shouyuerendataList = [];
      this.dataForm = {};
      this.addDialogData.visible = false;
    },
    addBtn() {
      this.index = this.dataList.length + 1;
      var item = {
        renwuName: "任务名称" + this.index,
        bkhName: "1",
        zycd: this.chengduList[0].id,
        wcnd: this.chengduList[0].id,
        weightset: "0",
        rwYxqq: this.dataForm.rwYxqq,
        rwYxqz: this.dataForm.rwYxqz,
        workTarget: "无",
        dataProvider: "1",
        providerName: "1",
        khrName:"",
        khrId:"",
        index: this.index,
      };
      this.dataList.push(item);
    },
    chooseBtn(row){
      this.sortingPerChooseDialogData={
        visible: true,
        types:"zdrw",
        perLsit:{
          userName:row.khrName,
          userId:row.khrId,
        },
        index:row.index
      }
    },
    sortingPerChooseDialogChild(data){
      this.dataList.forEach(ele=>{
        if(data.index == ele.index){
          ele.khrName = data.khrName,
          ele.khrId = data.khrId
        }
      })
    },
    reuseDialogChildren(data) {
      console.log(data);
      data.forEach((ele) => {
        var item = {
          renwuName: ele.renwuName,
          bkhName: ele.bkhName,
          zycd: ele.zycd,
          wcnd: ele.wcnd,
          weightset: ele.weightset,
          rwYxqq: ele.rwYxqq,
          rwYxqz: ele.rwYxqz,
          workTarget: ele.workTarget,
          dataProvider: ele.dataProvider,
          providerName: ele.providerName,
          index: this.index,
        };
        this.index++;
        this.dataList.push(item);
      });
    },
    // 复用
    reuseBtn() {
      this.reuseDialogData = {
        visible: true,
      };
    },
    handleFocus(startTime) {
      clickStartTime = startTime;
    },
    delBtn(row) {
      this.dataList.forEach((eles, i) => {
        if (row.index == eles.index) {
          this.dataList.splice(i, 1);
        }
      });
    },
    addUser(type) {
      this.jsonData = JSON.stringify(this.shouyuerendataList);
      this.$nextTick(() => {
        this.$refs.usercomment.init("12", type, this.jsonData, "htcj");
      });
    },
    submitfy(data) {
      this.shouyuerendataList = data.tableDataUser;
      var obj = {};
      this.shouyuerendataList.forEach((ele) => {
        var user = {};
        user.userId = ele.userId;
        user.userName = ele.userCode;
        user.ryName = ele.userName;
        user.deptId = ele.deptId;
        user.userDeptName = ele.deptName;
        user.userZw = ele.userZw;
        user.userLx = "自拟";
        user.userZj = ele.userZj;
        this.zdrwBkhUsers.push(user);
      });
    },
    fneffective() {
      effective({
        tagName: "重点任务",
      }).then((res) => {
        this.categoryList = res.data.data;
      });
    },
  },
};
</script>
<style scoped>
.cjrwBg {
  border-radius: 16px;
  -webkit-box-shadow: 5px 5px 10px #e3e3ea, -5px -5px 10px #ffffff;
  box-shadow: 5px 5px 10px #e3e3ea, -5px -5px 10px #ffffff;
  background: #fbfbfd;
  padding: 10px 30px 30px 30px;
}
</style>
